import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const scrollBehavior = (to, from, savedPosition) => {
  if (to.hash) {
    return {
      selector: to.hash
    }
  } else {
    const position = {}
    if (to.hash) {
      position.selector = to.hash
    }
    if (to.matched.some(m => m.meta.scrollToTop)) {
      position.x = 0
      position.y = 0
    }
    return position
  }
}

export default new Router({
  mode: 'history',
  base: 'front',
  scrollBehavior,
  routes: [
    {
      path: '/',
      component: () => import('@/pages/index'),
      children: [
        {
          path: '/',
          name: 'home',
          component: () => import('@/pages/home')
        },
        {
          path: '/category/:categoryId',
          name: 'category',
          component: () => import('@/pages/category')
        },
        {
          path: '/category/:categoryId/:childCategoryId',
          name: 'categoryWithChild',
          component: () => import('@/pages/category')
        },
        {
          path: '/goods/detail/:id',
          name: 'goodsDetail',
          component: () => import('@/pages/goodsDetail')
        },
        {
          path: '/cart',
          name: 'shoppingCart',
          component: () => import('@/pages/cart')
        },
        {
          path: '/my',
          component: () => import('@/pages/my'),
          children: [
            {
              path: '/order/list',
              name: 'orderList',
              component: () => import('@/pages/order/list')
            },
            {
              path: '/order/detail/:orderId',
              name: 'orderDetail',
              component: () => import('@/pages/order/detail')
            },
            {
              path: '/personal/center',
              name: 'personalCenter',
              component: () => import('@/pages/personalCenter')
            },
            {
              path: '*',
              name: 'myDefault',
              redirect: '/personal/center'
            }
          ]
        },
        {
          path: '/order/confirm',
          name: 'orderConfirm',
          component: () => import('@/pages/order/confirm')
        }
      ]
    },
    {
      path: '/404',
      name: 'page404',
      component: () => import('@/pages/404')
    },
    {
      path: '*',
      redirect: '/404',
      name: 'notFound',
      hidden: true
    }
  ]
})
